package com.leetcode.array;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

/**
 * Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
 *
 *Given nums = [2, 7, 11, 15], target = 9,

  Because nums[0] + nums[1] = 2 + 7 = 9,
  return [0, 1].
 * @author Adiy
 *
 */
public class TwoSum {

	public static void main(String[] args){
		int numarr[]={2,3,4};
        int target=6;
        int results[]=twoSum(numarr, target);
        System.out.println("{"+results[0]+","+results[1]+"}");
		
	}
	
    public static int[] twoSum1(int[] nums, int target) {
    	int len=nums.length;
    	int results[]=new int[2];
    	for(int i=0;i<len;i++){
    		for(int j=i+1;j<len;j++){
    			if(nums[i]+nums[j]==target){
    				results[0]=i;
    				results[1]=j;
    			}
    		}
    	}
		return results;
        
    }
    public static int[] twoSum(int[] nums, int target){
    	 Map <Integer, Integer> map = new HashMap<Integer, Integer>();
         for(int i=0;i<nums.length;i++) map.put(nums[i], i);
         for(int i=0;i<nums.length;i++){
             int needed = target - nums[i];
             if(map.containsKey(needed)  && map.get(needed) != i) return (new int[]{i+1,map.get(needed)+1});
         }
         return null;
    }
    
    @Test
    public void fun(){
    	int[] nums=new int[12598];
    	for(int i=0;i<nums.length;i++){
    		nums[i]=2*i;
    	}
    	int target=16021;
    	int[] result=twoSum1(nums, target);
    	System.out.println(result[0]+" "+result[1]);
    }
}
